<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class LiveMeetingRoom extends Model
{
	
    use SoftDeletes;

    protected $table = 'live_meeting_room';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    protected $appends = ['live_admin_name','list_img_url'];


    public function getListImgUrlAttribute()
    {
        return config('app.img_url') . $this ->list_img;
    }


    public function getLiveAdminNameAttribute()
    {
        $admin = AdminUsers::find($this -> live_admin_id);

        return $admin ? $admin -> name : '';
    }


    // 文章分类
    public function meeting_admin()
    {
        return $this->hasOne('\App\Models\AdminUsers', 'id', 'live_admin_id');
    }


    /**
     * 获取公共参数
     */
    public static function commonParam(){

        $timestamp    = time();
        $appId        = config('zego.zego_appid');
        $serverSecret = config('zego.zego_server_secret');


        $signatureNonce = bin2hex(random_bytes(8));
        $signature      = generateSignature($appId, $signatureNonce, $serverSecret,$timestamp);

        $commonParam = [
            'AppId'          => config('zego.zego_appid'),
            'SignatureNonce' => $signatureNonce,
            'Timestamp'      => $timestamp,
            'Signature'      => $signature,
            'SignatureVersion' => '2.0',
            'IsTest'         => true
        ];


        return http_build_query($commonParam);
    }


    public static function uniqudRoomNo(){

        $func = function()use(&$func){
            $room_no = rand(100000,999999);
            if (self::where('room_no',$room_no) -> first()) {
                return $func();
            } else {
                return $room_no;
            }
        };

        return $func();

    }
    
}
